"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具新建目录之-U UDS:socket_domain参数验证和执行节点验证
Create At   : 2023/4/13
Owner       : opentestcase019
Description :
    1.新建目录，指定-U UDS:socket_domain参数为正常值
    2.新建目录，指定错误的-U UDS:socket_domain参数
    3.新建目录，指定权限不足的socket_domain
    4.在备机执行新建目录
Expect      :
    1.创建成功
    2.合理报错Failed to get uds connection.
    3.合理报错Failed to get uds connection.
    4.创建成功
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.standby_node = Node('Standby1DbUser')
        self.common = Common()
        self.constant = Constant()
        self.dir_name_1 = "dir_dsscmd_0020_1"
        self.dir_name_2 = "dir_dsscmd_0020_2"
        self.logger.info("-----获取卷组名-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to display lsvg info.", execute_msg, "获取卷组名失败")
        self.vgname = execute_msg.splitlines()[1].split()[0].strip()
        self.logger.info(self.vgname)
        self.path_1 = os.path.join(self.vgname, self.dir_name_1)
        self.path_2 = os.path.join(self.vgname, self.dir_name_2)

    def test_sharestorage_tool(self):
        step1 = "-----step1:新建目录，指定-U UDS:socket_domain参数为正常值;expect:创建成功-----"
        self.logger.info(step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_1} " \
            f"-U {macro.UDS_PATH}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Succeed to make dir, path is +{self.vgname}, "
                      f"dir name is {self.dir_name_1}.",
                      execute_msg, "执行失败" + step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd ls -p +{self.vgname}"
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.dir_name_1, execute_msg, "执行失败" + step1)

        step2 = "-----step2:新建目录，指定错误的-U UDS:socket_domain参数;" \
                "expect:合理报错Failed to get uds connection.-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_2} " \
            f"-U {macro.UDS_PATH}.cfg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step2)

        step3 = "-----step3:新建目录，指定权限不足的socket_domain;" \
                "expect:合理报错Failed to get uds connection.-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"chmod 000 {macro.UDS_PATH[4::]};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_2} " \
            f"-U {macro.UDS_PATH};" \
            f"chmod 600 {macro.UDS_PATH[4::]}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step3)

        step4 = "-----step4:在备机执行新建目录;expect:创建成功-----"
        self.logger.info(step4)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_2} " \
            f"-U {macro.UDS_PATH}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn(f"Succeed to make dir, path is +{self.vgname}, "
                      f"dir name is {self.dir_name_2}.",
                      execute_msg, "执行失败" + step4)

    def tearDown(self):
        self.logger.info("-----恢复环境-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd rmdir -p +{self.path_1};" \
            f"dsscmd rmdir -p +{self.path_2}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Succeed to rm dir, path is +{self.path_1}",
                      execute_msg, "恢复环境失败")
        self.assertIn(f"Succeed to rm dir, path is +{self.path_2}",
                      execute_msg, "恢复环境失败")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
